#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cstdio>

using namespace std;
typedef pair<int, int> PII;
const int N = 105;
int q[N][N];
PII t[N * N];
int d[N][N];
bool st[N][N];
int tt, hh;
int n, m;
int bfs()
{
    hh = 0, tt = -1;
    int dx[4] = { 0, -1, 0, 1 }, dy[4] = { 1, 0, -1, 0 };
    t[++tt] = { 1, 1 };
    while (hh <= tt)
    {
        PII h = t[++hh];
        for (int i = 0; i < 4; i++)
        {
            int f = h.first + dx[i], s = h.second + dy[i];
            if (!q[f][s] && !st[f][s] && f > 0 && f <= n && s > 0 && s <= m)
            {
                t[++tt] = { f, s };
                d[f][s] = d[h.first][h.second] + 1;
                st[f][s] = true;
            }
        }
    }
    return d[n][m];
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%d", &q[i][j]);
    cout << bfs();
    return 0;
}